﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;

public partial class Logout : CE.Global.MyLanguageBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LogUserViewedHistory();
        }

        if (Session[CE.Global.General.UserId] != null)
        {
            //if (ClearUserLoginCached(Session[CE.Global.General.UserId].ToString()))
            //{

            if (Session[CE.Global.General.LoginHistoryId] != null)
                UpdateLogInHistory(Convert.ToInt64(Session[CE.Global.General.LoginHistoryId].ToString()));

            bool _isProvider = false;
            bool _isAgent = false;
            if (Session[CE.Global.General.UserTypeId].ToString() == "300")
            {
                _isProvider = true;
                AccountStatusDB _accountstatusdb = new AccountStatusDB();
                _accountstatusdb.UpdateLoginStatus(Convert.ToInt64(Session[CE.Global.General.UserId].ToString()));
            }

            if (Session[CE.Global.General.AgentId] != null)
            {
                _isAgent = true;
            }

            CE.Global.General.ClearAllSession(this.Page, "LOGOUT");

            if (_isProvider)
            {
                Response.Redirect("~/Provider/ProviderSecureLogin.aspx");

            }
            else if (_isAgent)
            {
                Response.Redirect("~/car-insurance/AgentLogin.aspx");
            }
            else
            {
                string _redirectUrl = "default.aspx";
                Response.Redirect(_redirectUrl);
            }

            //}
            //else
            //{
            //    CE.Global.General.ClearAllSession(this.Page, "LOGOUT");

            //    Response.Redirect("~/Home.aspx");
            //}
        }
        else
        {
            string _redirectUrl = "default.aspx";
            Response.Redirect(_redirectUrl);
        }
        
    }

    private void LogUserViewedHistory()
    {
        UserViewHistoryDB _userviewdb = new UserViewHistoryDB();
        _userviewdb.LogViewedHistory("USR", Request, "User log out.");
    }

    protected void UpdateLogInHistory(Int64 LogHisId)
    {
        LoginHistory _loghis = new LoginHistory();
        LoginHistoryDB _loghisdb = new LoginHistoryDB();

        _loghis.LoginHisId = LogHisId;
        _loghis.EndTime = DateTime.Now;
        _loghis.Status = "LOGIN/LOGOUT";
        _loghis.IsExpired = false;
        _loghisdb.Insert(_loghis);

        _loghis = null;
        _loghisdb = null;

    }
}
